<?php

namespace App\Http\Controllers\Backend;

use App\DataTables\ChannelUserDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateChannelUserRequest;
use App\Http\Requests\UpdateChannelUserRequest;
use App\Repositories\ChannelUserRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;

class ChannelUserController extends AppBaseController
{
    /** @var  ChannelUserRepository */
    private $channelUserRepository;

    public function __construct(ChannelUserRepository $channelUserRepo)
    {
        $this->channelUserRepository = $channelUserRepo;
    }

    /**
     * Display a listing of the ChannelUser.
     *
     * @param ChannelUserDataTable $channelUserDataTable
     * @return Response
     */
    public function index(ChannelUserDataTable $channelUserDataTable)
    {
        return $channelUserDataTable->render('backend.channel_users.index');
    }

    /**
     * Show the form for creating a new ChannelUser.
     *
     * @return Response
     */
    public function create()
    {
        return view('backend.channel_users.create');
    }

    /**
     * Store a newly created ChannelUser in storage.
     *
     * @param CreateChannelUserRequest $request
     *
     * @return Response
     */
    public function store(CreateChannelUserRequest $request)
    {
        $input = $request->all();

        $channelUser = $this->channelUserRepository->create($input);

        Flash::success(trans('flash.controller.save_success', ['model' => 'Channel User']));

        return redirect(route('channelUsers.index'));
    }

    /**
     * Display the specified ChannelUser.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $channelUser = $this->channelUserRepository->findWithoutFail($id);

        if (empty($channelUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel User']));

            return redirect(route('channelUsers.index'));
        }

        return view('backend.channel_users.show')->with('channelUser', $channelUser);
    }

    /**
     * Show the form for editing the specified ChannelUser.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $channelUser = $this->channelUserRepository->findWithoutFail($id);

        if (empty($channelUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel User']));

            return redirect(route('channelUsers.index'));
        }

        return view('backend.channel_users.edit')->with('channelUser', $channelUser);
    }

    /**
     * Update the specified ChannelUser in storage.
     *
     * @param  int              $id
     * @param UpdateChannelUserRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateChannelUserRequest $request)
    {
        $channelUser = $this->channelUserRepository->findWithoutFail($id);

        if (empty($channelUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel User']));

            return redirect(route('channelUsers.index'));
        }

        $channelUser = $this->channelUserRepository->update($request->all(), $id);

        Flash::success(trans('flash.controller.update_success', ['model' => 'Channel User']));

        return redirect(route('channelUsers.index'));
    }

    /**
     * Remove the specified ChannelUser from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $channelUser = $this->channelUserRepository->findWithoutFail($id);

        if (empty($channelUser)) {
            Flash::error(trans('flash.controller.not_found', ['model' => 'Channel User']));

            return redirect(route('channelUsers.index'));
        }

        $this->channelUserRepository->delete($id);

        Flash::success(trans('flash.controller.delete_success', ['model' => 'Channel User']));

        return redirect(route('channelUsers.index'));
    }
}
